﻿using MDT.LifeSewagePlatform.Conmmon;
using MDT.LifeSewagePlatform.InterfaceService;
using MDT.LifeSewagePlatform.Entity;
using MDT.LifeSewagePlatform.IRepository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MDT.LifeSewagePlatform.Service
{
    public class AlarmService:BaseService,IAlarmService
    {
        #region ctor and prop
        [Autowired]
        protected IAlarmRepository alarmRepository { get; set; }
        [Autowired]
        protected IDetailRepository detailRepository { get; set; }
        [Autowired]
        private SystemConfigEntity systemConfig { get; set; }
        public AlarmService()
        {
          
        }
        #endregion

        #region method service
        public async Task<DataMessage<AlarmView>> GetAlarms(AlarmParameter Params, CurrentUserInfo Userinfo)
        {

            IQueryable<AlarmView> alarms=null;

            if (systemConfig.QuarterOrMonth==(int)eGroupGetDateType.Quarter)
            {
                alarms = alarmRepository.GetAlarms(al=>al.Date.Year==Params.Year && al.Quarter==Params.Quarter);
            }
            if (systemConfig.QuarterOrMonth == (int)eGroupGetDateType.Month)
            {
                alarms = alarmRepository.GetAlarms(al => al.Date.Year == Params.Year && al.Date.Month == Params.Month);
            }
           
            if (IsChildAccount(Userinfo.RoleID))
            {
                alarms = alarms.Where(a => a.AreaID == Userinfo.CurrentAreaID);
            }
            else if (Params.AreaID>0)
            {
                alarms = alarms.Where(a=>a.AreaID==Params.AreaID);
            }
            else
            {
                if (await IdentityAuth.Auth(Userinfo.PlaceTypeName,Params.AreaID)==false)
                {
                    return BadRequest400<AlarmView>("权限无法查看市级数据!", null);
                }
            }

            if (await IdentityAuth.Admin(Userinfo.RoleName))
            {
                alarms = alarms.Where(t=>t.CompanyID==Params.CompanyID);
            }
            else
            {
                alarms = alarms.Where(t=>t.CompanyID==Userinfo.CompanyID);
            }
            return Ok200("查询成功!",await PageList<AlarmView>.CreateAsync(alarms,Params));
        }
        #endregion
    }
}
